//
//  FeedBackViewController.m
//  shuodi
//
//  Created by 钱坚 on 15/12/17.
//  Copyright © 2015年 Suzhou fish Information Technology Co., Ltd. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "FeedBackViewController.h"

@implementation FeedBackViewController
@synthesize descTv,contentPlaceholder,commitBtn;
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor colorWithRed:231.0/255.0 green:254.0/255.0 blue:1.0 alpha:1.0]];
    self.title = @"反馈建议";

    [self initData];
    [self initView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)sendClick:(id)sender{
    
}

-(void)initData{
    
    
}

- (void)initView{
    UIView *section = [[UIView alloc]initWithFrame:CGRectZero];
    section.backgroundColor = [UIColor whiteColor];
    section.layer.masksToBounds=YES;
    section.layer.cornerRadius = 4;
    section.layer.borderWidth = 0.5;
    section.layer.borderColor = [utils getBorderColorGray].CGColor;
    
    CGFloat y = 0;
    
    descTv = [[UITextView alloc]initWithFrame:CGRectMake(0, y,self.view.frame.size.width-OFFSET_TEN*2, 250)];
    descTv.backgroundColor = [UIColor clearColor];
    descTv.font =[UIFont systemFontOfSize:14];
    descTv.delegate = self;
    descTv.returnKeyType = UIReturnKeyDone;
//    descTv.layer.borderColor = [utils getBorderColorGray].CGColor;
//    descTv.layer.borderWidth =1.0;
    [section addSubview:descTv];
    
    contentPlaceholder = [[UILabel alloc]init];
    contentPlaceholder.frame =CGRectMake(5, descTv.frame.origin.y+5, 310, 20);
    contentPlaceholder.text = @"欢迎您给我们提供宝贵的改进产品及服务的建议！";
    contentPlaceholder.enabled = NO;//lable必须设置为不可用
    contentPlaceholder.font =[UIFont fontWithName:@"TrebuchetMS" size:14];
    contentPlaceholder.backgroundColor = [UIColor clearColor];
    contentPlaceholder.textColor = [UIColor colorWithRed:187.0/255 green:19.0/255 blue:14.0/255.0 alpha:1.0];
    [section addSubview:contentPlaceholder];
    
    y = [utils getViewBottom:descTv];
    
    section.frame = CGRectMake(OFFSET_TEN, 64+OFFSET_TEN, self.view.frame.size.width - OFFSET_TEN*2, y);
    [self.view addSubview:section];
    y = [utils getViewBottom:section];

    commitBtn = [utils getButton:CGRectMake(OFFSET_TEN, y+20, self.view.frame.size.width/2, 45) withBgColor:[UIColor whiteColor] withHighColor:nil withName:@"提交"];
    [commitBtn sizeToFit];
    [commitBtn setTitleColor:[utils getTextColorGray] forState:UIControlStateNormal];
    [commitBtn setFrame:CGRectMake( OFFSET_TEN, y+20, self.view.frame.size.width - OFFSET_TEN*2, 45)];
    [self.view addSubview:commitBtn];
    [commitBtn addTarget:self action:@selector(commit:) forControlEvents: UIControlEventTouchUpInside];
}

-(IBAction)commit:(id)sender{
    [self requestData];
}

-(void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length == 0) {
        contentPlaceholder.text = NSLocalizedString(@"add_comment_placeholder", nil);
    }else{
        contentPlaceholder.text = @"";

    }
    
    NSString *toBeString = [textView text];
    
    //    if (descTv == textView)  //判断是否时我们想要限定的那个输入框
    //    {
    //        NSUInteger kMaxLength = 100;
    //        NSString *lang = [[UIApplication sharedApplication]textInputMode].primaryLanguage; // 键盘输入模式
    //        if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入，包括简体拼音，健体五笔，简体手写
    //            UITextRange *selectedRange = [textView markedTextRange];
    //            //获取高亮部分
    //            UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
    //            // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
    //            if (!position) {
    //                if (toBeString.length > kMaxLength) {
    //                    textView.text = [toBeString substringToIndex:kMaxLength];
    //                    [OMGToast showWithText:@"超过最大字数不能输入了"  duration:1.5];
    //                }
    //            }
    //            // 有高亮选择的字符串，则暂不对文字进行统计和限制
    //            else{
    //
    //            }
    //        }
    //        // 中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
    //        else{
    //            if (toBeString.length > kMaxLength) {
    //                textView.text = [toBeString substringToIndex:kMaxLength];
    //                [OMGToast showWithText:@"超过最大字数不能输入了"  duration:1.5];
    //            }
    //        }
    //
    //        NSString *value = [NSString stringWithFormat:@"%lu",(unsigned long)[textView.text length]];
    //    }
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
    
}

- (IBAction)editingChanged:(id)sender{
    
    UITextField *textField = (UITextField *)sender;
    NSString *toBeString = [textField text];
    
//    if (groupTitleTF == textField)  //判断是否时我们想要限定的那个输入框
//    {
//        NSUInteger kMaxLength = 10;
//        NSString *lang = [[UIApplication sharedApplication]textInputMode].primaryLanguage; // 键盘输入模式
//        if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入，包括简体拼音，健体五笔，简体手写
//            UITextRange *selectedRange = [textField markedTextRange];
//            //获取高亮部分
//            UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
//            // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
//            if (!position) {
//                if (toBeString.length > kMaxLength) {
//                    textField.text = [toBeString substringToIndex:kMaxLength];
//                    [OMGToast showWithText:@"超过最大字数不能输入了"  duration:1.5];
//                }
//            }
//            // 有高亮选择的字符串，则暂不对文字进行统计和限制
//            else{
//                
//            }
//        }
//        // 中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
//        else{
//            if (toBeString.length > kMaxLength) {
//                textField.text = [toBeString substringToIndex:kMaxLength];
//                [OMGToast showWithText:@"超过最大字数不能输入了"  duration:1.5];
//            }
//        }
//        
//        NSString *value = [NSString stringWithFormat:@"%lu",(unsigned long)[textField.text length]];
//    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];    //主要是[receiver resignFirstResponder]在哪调用就能把receiver对应的键盘往下收
    return YES;
}

-(UIImageView*)dashLineView:(CGRect)frame parentView:(UIView*)view{
    UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:frame];
    [view addSubview:imageView1];
    
    UIGraphicsBeginImageContext(imageView1.frame.size);   //开始画线
    [imageView1.image drawInRect:CGRectMake(0, 0, imageView1.frame.size.width, imageView1.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);  //设置线条终点形状
    
    
    CGFloat lengths[] = {2,2};
    CGContextRef line = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(line, [UIColor grayColor].CGColor);
    
    CGContextSetLineDash(line, 0, lengths, 2);  //画虚线
    CGContextMoveToPoint(line, 0.0, imageView1.frame.size.height);    //开始画线
    CGContextAddLineToPoint(line, frame.size.width, imageView1.frame.size.height);
    CGContextStrokePath(line);
    //    CGContextClosePath(line);
    imageView1.image = UIGraphicsGetImageFromCurrentImageContext();
    return imageView1;
}

-(void)requestData{
    __weak typeof(self) weakSelf = self;
    
    if ([descTv.text isEqualToString:@""]) {
        [OMGToast showWithText:@"反馈内容不能为空" bottomOffset:50 duration:1];
        return;
    }
    NSDictionary *dic = @{@"content":descTv.text};
//    [HttpNetWorkUtil requestadvisesave:dic success:^(id json) {
//        if (json) {
//            __strong __typeof(weakSelf)strongSelf = weakSelf;
////            NSString *message =[json objectForKey:@"message"];
//            [OMGToast showWithText:@"反馈成功" duration:1.0];
//            [strongSelf.navigationController popViewControllerAnimated:YES];
//        }
//        
//    } fail:^{}];
}
@end